package com.atguigu.spzx.manager.controller;

import com.atguigu.spzx.common.log.annotation.Log;
import com.atguigu.spzx.common.log.service.AsyncOperLogService;
import com.atguigu.spzx.manager.service.BrandService;
import com.atguigu.spzx.model.entity.product.Brand;
import com.atguigu.spzx.model.entity.system.SysOperLog;
import com.atguigu.spzx.model.entity.system.SysUser;
import com.atguigu.spzx.model.vo.common.Result;
import com.atguigu.spzx.model.vo.common.ResultCodeEnum;
import com.github.pagehelper.PageInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author syh
 * @create 2023-08-21 19:43
 */
@RestController
@Tag(name = "商品品牌管理模块")
@RequestMapping(value="/admin/product/brand")
public class BrandController {

    @Autowired
    private BrandService brandService ;
    @Resource
    AsyncOperLogService asyncOperLogService;

    @Operation(summary = "查询所有品牌")
    @GetMapping
    public Result<List<Brand>> listAll(){
        List<Brand> brands=brandService.listAll();
        return Result.build(brands,ResultCodeEnum.SUCCESS);
    }


    @Operation(summary = "删除")
    @DeleteMapping("{id}")
    public Result deleteById(@PathVariable("id")Long id){
        brandService.deleteById(id);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }
    @Operation(summary = "新增")
    @PostMapping
    public Result save(@RequestBody Brand brand){
        brandService.save(brand);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "根据id查询")
    @GetMapping("{id}")
    public Result<SysUser> getById(@PathVariable("id")Long id){
        Brand brand=brandService.getById(id);
        return Result.build(brand,ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "更新")
//    @Log(title = "更新品牌",businessType = 2)
    @PutMapping
    public Result updateById(@RequestBody Brand brand){
        brandService.updateById(brand);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }



    @Operation(summary = "分页查询品牌")
    @GetMapping("page/{pageNum}/{pageSize}")
    public Result<PageInfo<Brand>> findByPage(@PathVariable("pageNum") Integer pageNum, @PathVariable("pageSize") Integer pageSize) {
        PageInfo<Brand> pageInfo = brandService.getPage(pageNum, pageSize);
        //记录查询的日志
        SysOperLog sysOperLog=new SysOperLog();
        asyncOperLogService.saveSysOperLog(sysOperLog);
        return Result.build(pageInfo , ResultCodeEnum.SUCCESS) ;
    }

}
